<?php

namespace App\Http\Service;

use App\Util\Result;
use App\Model\Role;

trait ServiceRole{

    /**
     * @return mixed
     * 角色列表
     */
    public static function getList($title,$limit){
        if (empty($title)) {
            $res = Role::orderBy('id','desc')->paginate($limit);
        } else {
            $res = Role::orderBy('id','desc')->where("title","like", '%'.$title.'%')->paginate($limit);
        }
        return Result::success()->setData($res);
    }

    /**
     *获取单个id记录
     */
    public static function getOne($id) {
        $res = Role::where("id", $id)->first();
        return Result::success()->setData($res);
    }

    /**
     *更新角色
     */
    public static function updateData($input, $id) {
        $res = Role::where("id", $id)->update($input);
        return Result::success()->setData($res);
    }

    /**
     *删除记录
     */
    public static function deleteRole($id) {
        $res = Role::where("id", $id)->delete();
        return Result::success()->setData($res);
    }

    /**
     * @param $id
     * 禁用、启用
     */
    public static function able($id,$able) {
        Role::where("id", $id)->update(['status' => $able]);
        if ($able==2) {
            return Result::success()->setMsg("启用")->setData(2);
        } else {
            return Result::success()->setMsg("禁用")->setData(1);
        }
    }

    /**
     * 角色下拉
     */
    public static function roleList() {
        $res = Role::select('id', 'title')->where('status',1)->get();
        return Result::success()->setData($res);
    }
}


